package com.av.hibernate.util;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.HibernateException;
import org.hibernate.usertype.UserType;

import com.av.db.dataobjects.Idioma;

/**
 * Clase creada para poder mapear y guardar los valores de idioma
 * 
 * @author Victor J Morales R
 * 
 */
public class IdiomaUserType implements UserType {

	private static final int[] SQL_TYPES = { Types.INTEGER };

	@Override
	public Object assemble(Serializable arg0, Object arg1)
			throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object deepCopy(Object value) throws HibernateException {
		return value;
	}// deepCopy

	@Override
	public Serializable disassemble(Object arg0) throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean equals(Object x, Object y) throws HibernateException {
		if (x == y) {
			return true;
		} else if (x == null || y == null) {
			return false;
		} else {
			return x.equals(y);
		}
	}// equals

	@Override
	public int hashCode(Object arg0) throws HibernateException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean isMutable() {
		return false;
	}// isMutable

	@Override
	public Object nullSafeGet(ResultSet resultSet, String[] names, Object owner)
			throws HibernateException, SQLException {

		int iIdioma = resultSet.getInt(names[0]);
		return resultSet.wasNull() ? null : Idioma.getIdioma(iIdioma);
	}// nullSafeGet

	@Override
	public void nullSafeSet(PreparedStatement statement, Object value, int index)
			throws HibernateException, SQLException {

		if (value == null) {
			statement.setInt(index, 0);
		} else if (value instanceof Idioma) {
			Idioma i = (Idioma) value;
			statement.setInt(index, i.getId());
		}
	}// nullSafeSet

	@Override
	public Object replace(Object arg0, Object arg1, Object arg2)
			throws HibernateException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Class<Idioma> returnedClass() {
		return Idioma.class;
	}// returnedClass

	@Override
	public int[] sqlTypes() {
		return SQL_TYPES;
	}// sqlTypes

}// IdiomaUserType